package br.trainee.web.componentes;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import br.trainee.web.util.ResponseWrapper;

public class PanelTag extends BodyTagSupport{
	
	private String id;
	private String modal;
	private String backdrop;
	private String keyboard;
	private String show;
	private String width;
	private String customClass;
	private String close;


	@Override
	public int doEndTag() throws JspException {
		 
		String body = getBodyContent().getString();
		
		JspWriter saida = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
		
		request.setAttribute("id", id);
		request.setAttribute("modal", modal);
		request.setAttribute("backdrop", backdrop);
		request.setAttribute("keyboard", keyboard);
		request.setAttribute("show", show);
		request.setAttribute("width", width);
		request.setAttribute("customClass", customClass);
		request.setAttribute("close", close);
		request.setAttribute("body", body);
		
		ResponseWrapper responseWrapper = new ResponseWrapper(response);
		
		RequestDispatcher req = request.getRequestDispatcher("/resources/componentes-jsp/panel.jsp");
		
		try {
			
			//faz a mesma função do forward, so que podendo fazer alterações apos enviar a requisição
			req.include(request, responseWrapper);
			String conteudoSaida = responseWrapper.getString();
			saida.println(conteudoSaida);
			
		} catch (ServletException | IOException e1) {

			e1.printStackTrace();
		}
		
		return super.doEndTag();
	}

	public String getModal() {
		return modal;
	}

	public void setModal(String modal) {
		this.modal = modal;
	}

	public String getBackdrop() {
		return backdrop;
	}

	public void setBackdrop(String backdrop) {
		this.backdrop = backdrop;
	}

	public String getKeyboard() {
		return keyboard;
	}

	public void setKeyboard(String keyboard) {
		this.keyboard = keyboard;
	}

	public String getShow() {
		return show;
	}

	public void setShow(String show) {
		this.show = show;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getCustomClass() {
		return customClass;
	}

	public void setCustomClass(String customClass) {
		this.customClass = customClass;
	}

	public String getClose() {
		return close;
	}

	public void setClose(String close) {
		this.close = close;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
}
